// common-mixin.vue
import {getStore} from "../assets/js/storage";
import config from "../config/config";

export default {
    data() {
        return {}
    },
    methods: {
        routerLink(page, replace = false) {
            if (replace) {
                this.$router.replace(page);
            } else {
                this.$router.push(this.fixFromWorkspaceUrl(page));
            }
        },
        toHome() {
            const currentOrganization = getStore('currentOrganization', true);
            let home = config.HOME_PAGE;
            if (currentOrganization) {
                home = home + '/' + currentOrganization.code;
            }
            if(home.indexOf('?') >= 0) {
                home += '&init=3'
            } else {
                home += '?init=3'
            }
            this.$router.push(this.fixFromWorkspaceUrl(home));
        },
        fixFromWorkspaceUrl(url) {
            if(url.indexOf('fromworkspace=1') < 0 && window.location.href.indexOf('fromworkspace=1') >= 0) {
                if(url.indexOf('?') >= 0) {
                    url += '&fromworkspace=1'
                } else {
                    url += '?fromworkspace=1'
                }
            }
            return url;
        },
        appendFromWorkspaceUrl(url) {
            if(url.indexOf('fromworkspace=task_detail') >= 0) {
                url = url.replace('fromworkspace=task_detail', 'fromworkspace=1')
            }
            if(url.indexOf('fromworkspace=1') < 0) {
                if(url.indexOf('?') >= 0) {
                    url += '&fromworkspace=1'
                } else {
                    url += '?fromworkspace=1'
                }
            }
            return url;
        },
        appendFromTaskDetailWorkspaceUrl(url) {
            if(url.indexOf('fromworkspace=1') >= 0) {
                url = url.replace('fromworkspace=1', 'fromworkspace=task_detail')
            }
            if(url.indexOf('fromworkspace=task_detail') < 0) {
                if(url.indexOf('?') >= 0) {
                    url += '&fromworkspace=task_detail'
                } else {
                    url += '?fromworkspace=task_detail'
                }
            }
            return url;
        },
        appendFromWorkspaceUrlForHomeTaskDetail(url) {
            if(this.fromWorkspace()) {
                return this.appendFromTaskDetailWorkspaceUrl(url)
            } else {
                return this.appendFromWorkspaceUrl(url)
            }
        },
        fromWorkspace(url) {
            if(!url) {
                url = window.location.href;
            }
            return url.indexOf('fromworkspace=1') >= 0
        },
        fromTaskDetailWorkspace(url) {
            if(!url) {
                url = window.location.href;
            }
            return url.indexOf('fromworkspace=task_detail') >= 0
        },
        targetFromWorkspace() {
            if(window.location.href.indexOf('fromworkspace=') >= 0) {
                return '';
            } else {
                return '_blank';
            }
        },
        getUrlQuery(name) {
            let url = window.location.href
            if(url.indexOf('?') > 0) {
                url = url.split('?')[1]
            }
            let urls = url.split('&')
            for(let i = 0; i < urls.length; i++) {
                url = urls[i]
                let ns = url.split('=')
                if(ns[0] == name) {
                    if(ns.length > 1) {
                        return ns[1]
                    }
                    return ''
                }
            }
            return undefined
        },
        fixResList(res) {
            if(res && res.data) {
                if(!res.data.list) {
                    res.data.list = []
                }
                if(!res.data.total) {
                    res.data.total = 0
                }
            }
        },
        opengiturl (url, type) {
            return window.opengiturl(url, type)
        },
    }
}
